//
//  DiscoverViewController.m
//  HeartOfMeet
//
//  Created by qianfeng on 16/3/30.
//  Copyright © 2016年 千云锋. All rights reserved.
//

#import "DiscoverViewController.h"
#import "DiscoverCollectionViewCell.h"
#import "DiscoverCircleModel.h"
#import "CircleView.h"
#import "HeaderView.h"
#import "NetDataEngine.h"
#import "EGOCache.h"



@interface DiscoverViewController ()<UITableViewDataSource, UITableViewDelegate ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>

@property (nonatomic) UICollectionView *myCollectionView1;

@property (nonatomic, strong) NSMutableArray *dataDataSource;
@property (nonatomic, strong) CircleView *circleView;

@property (nonatomic, strong) NSMutableArray *circleDataSource;
@property (nonatomic, strong) NSMutableArray *circleArray;

@property (nonatomic, strong) NSArray *categoryImageArray;
@property (nonatomic, strong) NSArray *categoryTitleArray;
@property (nonatomic, strong) NSArray *mindImageArray;
@property (nonatomic, strong) NSArray *mindTitleArray;
@property (nonatomic, strong) NSArray *sceneImageArray;
@property (nonatomic, strong) NSArray *sceneTitleArray;

@property (nonatomic) CGSize myItemSize1;

@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataDataSource = [[NSMutableArray alloc] init];
    self.circleDataSource = [[NSMutableArray alloc] init];
    self.circleArray = [[NSMutableArray alloc] init];
    
    self.categoryImageArray = @[@"自我成长.png", @"情绪管理.png", @"人际沟通.png", @"恋爱婚姻.png", @"职场管理.png", @"亲子家庭.png", @"心理科普.png", @"课程讲座.png"];
    self.categoryTitleArray = @[@"自我成长", @"情绪管理", @"人际沟通", @"恋爱婚姻", @"职场管理", @"亲子家庭", @"心理科普", @"课程讲座"];
    
    self.mindImageArray = @[@"kuaile.png", @"beishang.png", @"wunai.png", @"gudu.png", @"jianya.png", @"fanzao.png", @"gandong.png", @"mimang.png"];
    self.mindTitleArray = @[@"快乐", @"悲伤", @"无奈", @"孤独", @"减压", @"烦躁", @"感动", @"迷茫"];
    
    self.sceneImageArray = @[@"shuiqian.png", @"sanbu.png", @"shimian.png", @"wuliao.png", @"lvxing.png", @"duchu.png", @"shilian.png", @"zuoche.png"];
    self.sceneTitleArray = @[@"睡前", @"散步", @"失眠", @"无聊", @"旅行", @"独处", @"失恋", @"坐车"];
    
    _myItemSize1 = [LZXHelper imageSize:@"90" height:@"90" restictWidth:(kScreenWidth - 0 * 2 - 1 * 3)/4];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kScreenHeight - 64 - 49) style:UITableViewStyleGrouped];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    
    _tableView.backgroundColor = [UIColor blackColor];
    _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    [self.view addSubview:_tableView];
    
    [self fetchNetData];
    [self createCollectionView1];
    [self createScrollView];
}

- (void)fetchNetData{
//-------------------------EGO缓存------------------
    //如果有缓存
    if ([[EGOCache globalCache] hasCacheForKey:@"DiscoverCache"]) {
        id cachData = [[EGOCache globalCache] plistForKey:@"DiscoverCache"];
//        NSArray *array = [cachData objectForKey:@""]
        self.circleArray = [DiscoverCircleModel parseRespondsData:cachData];
        self.circleView.circleDataSource = self.circleArray;
        [self.tableView reloadData];
        
    }
    //如果没有缓存
    
    [[NetDataEngine sharedInstance] requestDiscoverUrl:DISCOVERSCROLLVIEWURL success:^(id respondObject) {
        //保存下载后的数据
        [[EGOCache globalCache] setPlist:respondObject forKey:@"DiscoverCache"];
        
        //
        self.circleArray = [DiscoverCircleModel parseRespondsData:respondObject];
        self.circleView.circleDataSource = self.circleArray;
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)createCollectionView1{
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc] init];
    layout1.minimumInteritemSpacing = 1;
    layout1.minimumLineSpacing = 1;
    
    layout1.itemSize = _myItemSize1;
    layout1.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _myCollectionView1 = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , _myItemSize1.height * 2 * 3 + 30 * 3 + 1 * 3) collectionViewLayout:layout1];
    
    _myCollectionView1.delegate = self;
    _myCollectionView1.dataSource = self;
    _myCollectionView1.tag = 999;
    _myCollectionView1.backgroundColor = [UIColor whiteColor];
    _myCollectionView1.scrollEnabled = NO;
    
    [_myCollectionView1 registerNib:[UINib nibWithNibName:@"DiscoverCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"DiscoverCollectionViewCell"];
    
//    //    注册_collectionView分组的头视图
//    //    提前告诉_collectionView用什么样的视图作为分组头视图的复用视图
    [_myCollectionView1 registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
   
}

#pragma mark - UICollectionViewDataSource

//返回有多少组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 8;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        static NSString *identifier = @"DiscoverCollectionViewCell";
        DiscoverCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithRed:20/255.0 green:20/255.0 blue:20/255.0 alpha:1.0];
    
        if (indexPath.section == 0) {
            cell.myImageView.image = [UIImage imageNamed:_categoryImageArray[indexPath.item]];
            cell.myLabel.text = _categoryTitleArray[indexPath.item];
            cell.tag = 10 + indexPath.item;

            
        }else if (indexPath.section == 1) {
            cell.myImageView.image = [UIImage imageNamed:_mindImageArray[indexPath.item]];
            cell.myLabel.text = _mindTitleArray[indexPath.item];
            cell.tag = 100 + indexPath.item;

        }else if (indexPath.section == 2) {
            cell.myImageView.image = [UIImage imageNamed:_sceneImageArray[indexPath.item]];
            cell.myLabel.text = _sceneTitleArray[indexPath.item];
            cell.tag = 1000 + indexPath.item;

        }
        return cell;
}

//设置组标题
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            static NSString *identifier = @"header";
            HeaderView *topView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:identifier forIndexPath:indexPath];
            topView.backgroundColor = [UIColor redColor];
            
            if (indexPath.section == 0) {
                topView.title = @"心灵之约     分类";
            }else if (indexPath.section == 1){
                topView.title = @"心灵之约     心情";
            }else if (indexPath.section == 2){
                topView.title = @"心灵之约     场景";
            }
            return topView;
    }
    return nil;
}
//组标题 行高  返回header的size，竖向滚动时，只需要写 y
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(0, 30);
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"];
        }
    
        [cell.contentView addSubview:self.myCollectionView1];
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _myItemSize1.height * 2 * 3 + 30 * 3 + 1 * 3;
    
}

//组头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

#pragma mark - 创建头部滚动视图
- (void)createScrollView{
    self.circleView = [CircleView circleViewFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 200)];
    self.circleView.userInteractionEnabled = YES;
    self.tableView.tableHeaderView = self.circleView;
    self.tableView.tableHeaderView.backgroundColor = [UIColor purpleColor];
}

#pragma mark - 选中item 时触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CategoryViewController *categoryVC = [[CategoryViewController alloc] init];
    
    DiscoverCollectionViewCell *cell = (DiscoverCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        categoryVC.cellTag = 10 + indexPath.item;
        categoryVC.cellSection = 0;
        
        
    }else if (indexPath.section == 1) {
        categoryVC.cellTag = 100 + indexPath.item;
        categoryVC.cellSection = 1;
    }else if (indexPath.section == 2) {
        categoryVC.cellTag = 1000 + indexPath.item;
        categoryVC.cellSection = 2;
    }
    categoryVC.name = cell.myLabel.text;
    categoryVC.navgiationBarTitle = cell.myLabel.text;
    categoryVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:categoryVC animated:YES];

}


@end
